package com.itboye.shop.me.qianbao;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.UserPresenter;
import com.itboye.shop.util.helper.utils.TimeCount;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.MAlert;
import com.itboye.shop.util.loadutil.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 忘记原支付密码(1,获取短信验证码)
 */
public class WangJiZhiFuPassActivity extends BaseActivity implements Observer{
    TextView tvTitle;
    ImageView back;
    private TimeCount time;// 倒计时
    TextView wangji_pass_phone;//手机号
    TextView wangji_pass_send;//发送验证码
    EditText wangji_pass_yzm;
    TextView pass_one_ok;//下一步
    String mobile;//手机号

    UserPresenter userPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wang_ji_zhi_fu_pass);
        tvTitle.setText("找回支付密码");

        mobile = (String) SPUtils.get(this, null, Const.MOBILE, "");

        if (!mobile.equals("")) {
            try {
                String hintphone = mobile.substring(0, 3) + "****"
                        + mobile.substring(7, 11);
                wangji_pass_phone.setText("请输入" + hintphone + "收到的短信校验码");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            wangji_pass_phone.setText("");
        }
        userPresenter = new UserPresenter(this);
        //支付密码修改成功的广播
        IntentFilter intentFilterExit = new IntentFilter(JiDePassTwoActivity.ZHI_FU_PASS_OK);
        registerReceiver(xiugaiPassBroadcastReceiver, intentFilterExit);
    }
    BroadcastReceiver xiugaiPassBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            finish();
        };
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(xiugaiPassBroadcastReceiver);
    }
    @Override
    public void onClick(View view) {
        Intent intent=null;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.wangji_pass_send://发送验证码
                userPresenter.sendVerificationCode("+86", mobile, 2 + "");
                time = new TimeCount(60000, 1000, wangji_pass_send);// 构造CountDownTimer对象
                time.start();
                break;
            case R.id.pass_one_ok:
                userPresenter.YanZhengverificationCode("+86", mobile, wangji_pass_yzm.getText().toString(), 2 + "");

                break;

            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {

            if (entity.getEventType() == UserPresenter.send_code_success) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
            }
            if (entity.getEventType() == UserPresenter.send_code_fail) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
            }
            if (entity.getEventType() == UserPresenter.yanzheng_code_success) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
                Intent intent = new Intent(this, WangJiZhiFuPassTwoActivity.class);
                startActivity(intent);
            }
            if (entity.getEventType() == UserPresenter.yanzheng_code_fail) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
            }
        }
    }
}
